#include "../menu.h"
#include "user.h"
#include "aw_pic_util.h"
#include "logo_dat.h" 
#include "aw_PIC_app.h"
pic_page_request_info_t six_pic_focus ;
int OldFocus=0;
pic_page_request_info_t* MutiPicFocusOperation(INT8U line,INT8U row,INT32U *key_value)
{
    int page_unit= line*row;    
    int page_offset = aw_title_num%page_unit;
    int row_offset = page_offset%row;
    int temp_offset =0 ;
    int a;
    OldFocus=aw_title_num;
    switch(*key_value){
    case UOP_LEFT:
	if(aw_title_num==0)
	{
		six_pic_focus.request_type=LAST_PAGE;
		six_pic_focus.request_index = ((TotalPicNum-1)/page_unit)*page_unit;
		six_pic_focus.request_focus= (TotalPicNum-1)%page_unit;
		*key_value=UOP_PREVIOUS_CHAP;    	   
     	}
	else if(page_offset==0)
	{
		six_pic_focus.request_type   = NORMAL_PAGE;
		six_pic_focus.request_focus  = page_unit-1;
		six_pic_focus.request_index  = aw_title_num-page_unit;
		*key_value=UOP_PREVIOUS_CHAP;
    	}
	else
	{
		six_pic_focus.request_type=NO_PAGE;
    	}
       break;
    case UOP_RIGHT:
	if((aw_title_num+1)>=TotalPicNum)
	{
		six_pic_focus.request_type=FIRST_PAGE;
		six_pic_focus.request_focus=0;
		six_pic_focus.request_index=0;
		*key_value=UOP_NEXT_CHAP;
	}
	else if(((page_offset+1)%page_unit)== 0)
	{
		six_pic_focus.request_type=NORMAL_PAGE;
		six_pic_focus.request_index= aw_title_num+1;
		six_pic_focus.request_focus= 0;		   
		*key_value=UOP_NEXT_CHAP;
	}
	else
	{
		six_pic_focus.request_type=NO_PAGE;
	}
	break;
    case UOP_UP:	     
	if((page_offset/row)==0)
	{	        
		if((aw_title_num/page_unit) == 0)
		{
			six_pic_focus.request_type=LAST_PAGE;
			six_pic_focus.request_index = ((TotalPicNum-1)/page_unit)*page_unit;
			temp_offset = ((TotalPicNum-1)%page_unit)%row;/*row offset*/
			if(temp_offset < row_offset)
			{                               
				six_pic_focus.request_focus= (TotalPicNum-1)%page_unit;
			}
			else
			{
				a =  ((TotalPicNum-1)%page_unit)/row +1;
				six_pic_focus.request_focus = (a-1)*row+row_offset;
			}
			*key_value=UOP_PREVIOUS_CHAP;
		}
		else
		{	        	        
			six_pic_focus.request_type=NORMAL_PAGE;            
			six_pic_focus.request_index=(aw_title_num/page_unit)*page_unit - page_unit;
			six_pic_focus.request_focus= (row*(line-1))+ page_offset%row;            
			*key_value=UOP_PREVIOUS_CHAP;
		}
	}
	else
	{
		six_pic_focus.request_type=NO_PAGE;
	}
	break;	
    case UOP_DOWN:    
	if(((page_offset/row)+1)== line)
	{	       
            if(((aw_title_num/page_unit)*page_unit +page_unit)>=TotalPicNum){
                six_pic_focus.request_type=FIRST_PAGE;
                six_pic_focus.request_index=0;                
                if(TotalPicNum > page_unit){
                    temp_offset =  row-1;       
                }else{
                    temp_offset = ((TotalPicNum-1)%page_unit)%row;/*row offset*/
                }                
                if(temp_offset < row_offset){                               
                six_pic_focus.request_focus=0;
     	        }else{
     	            six_pic_focus.request_focus = row_offset;
     	        }                             
                *key_value=UOP_NEXT_CHAP;
            }else{            
                six_pic_focus.request_type=NORMAL_PAGE;
                six_pic_focus.request_focus=page_offset%row;
                six_pic_focus.request_index=  (aw_title_num/page_unit)*page_unit + page_unit;
                *key_value=UOP_NEXT_CHAP;
            }
	}
	else if(((aw_title_num/page_unit)*page_unit+(row*((page_offset/row)+1 )))>= TotalPicNum)
	{
		six_pic_focus.request_type=FIRST_PAGE;
		six_pic_focus.request_index=0;
		if(TotalPicNum > page_unit)
		{
			temp_offset =  row-1;       
		}
		else
		{
			temp_offset = ((TotalPicNum-1)%page_unit)%row;/*row offset*/
		}                
		if(temp_offset < row_offset)
		{                               
			six_pic_focus.request_focus=0;
		}
		else
		{
			six_pic_focus.request_focus = row_offset;
		}  
		*key_value=UOP_NEXT_CHAP;
	}
	else
	{
		six_pic_focus.request_type=NO_PAGE;
	}
	break;
    case UOP_FAST_FORWARD:	
    case UOP_NEXT_CHAP:
		if(((aw_title_num/page_unit)*page_unit +page_unit)>=TotalPicNum)
		{
			six_pic_focus.request_type=FIRST_PAGE;
			six_pic_focus.request_focus=0;
			six_pic_focus.request_index=0 ;             
			*key_value=UOP_NEXT_CHAP;
		}
		else
		{
			six_pic_focus.request_type=NORMAL_PAGE;
			six_pic_focus.request_focus=0;
			six_pic_focus.request_index=(aw_title_num/page_unit)*page_unit + page_unit;
			*key_value=UOP_NEXT_CHAP;
		}
	break;
    case UOP_FAST_BACKWARD:
    case UOP_PREVIOUS_CHAP:
		if((aw_title_num/page_unit) == 0)
		{
			six_pic_focus.request_type=LAST_PAGE;
			six_pic_focus.request_index = ((TotalPicNum-1)/page_unit)*page_unit;
			six_pic_focus.request_focus= 0;
			*key_value=UOP_PREVIOUS_CHAP; 
		}
		else
		{
			six_pic_focus.request_type=NORMAL_PAGE;
			six_pic_focus.request_focus=0;
			six_pic_focus.request_index=(aw_title_num/page_unit)*page_unit - page_unit;
			*key_value=UOP_PREVIOUS_CHAP;
		}
	break;
    default:
	break;
    }
    if(six_pic_focus.request_type == NORMAL_PAGE)
    {
	if((six_pic_focus.request_index + six_pic_focus.request_focus+1)> TotalPicNum)
	{
		six_pic_focus.request_focus= (TotalPicNum - six_pic_focus.request_index-1);    
	}
    }
    if(six_pic_focus.request_type != NO_PAGE)
    {
	aw_title_num = six_pic_focus.request_index;
    }
    return &six_pic_focus;
}

/*after delete one file when muti-preview mode, we should re-create the playlist ,and need return to the current page
    in this case, should remember the start index of the current page
*/
INT32U get_page_start_index_by_prefile(INT8U line,INT8U row,INT32U index)
{
    int page_start_index = 0 ;
    int page_unit= line*row;    
	if(index <= 0) 
	{
		page_start_index =0;  
		return  page_start_index;
	}  
	if((index%page_unit) ==0 )
	{
		page_start_index = index - page_unit;    
	}
	else
	{
		page_start_index = (index/page_unit)*page_unit;
	}
	return page_start_index; 	
}

INT32U get_page_start_index_by_nextfile(INT8U line,INT8U row,INT32U index)
{

    int page_start_index = 0 ;
    int page_unit= line*row;    
	if(index >= (TotalPicNum-1))
	{
		page_start_index =0;  
		return  page_start_index; 
	}
	if((index%page_unit) ==0 )
	{
		page_start_index = index;  
	}
	else
	{
		page_start_index = (index/page_unit)*page_unit; 
	} 
	return page_start_index; 	
}


/*after delete one file when muti-preview mode, we should re-create the playlist ,and need return to the current page
    in this case, and highlight the previous file.
*/
INT32U get_page_focus_by_prefile(INT8U line,INT8U row,INT32U index)
{
    int page_focus = 0 ;
    int page_unit= line*row;    
	if(index <= 0)
	{
		page_focus =0;  
		return  page_focus;
	}  
	if((index%page_unit) ==0 )
	{
		page_focus = page_unit-1;    
	}
	else
	{
		page_focus = (index%page_unit)-1;
	}    
	return page_focus; 	
}

INT32U get_page_focus_by_nextfile(INT8U line,INT8U row,INT32U index)
{
    int page_focus = 0 ;
    int page_unit= line*row;    
	if(index >= (TotalPicNum-1))
	{
		page_focus =0;  
		return  page_focus;
	}
	page_focus = index%page_unit;    
	return page_focus; 	
}